var contact = function () {
    var contactInfo = this;

    this.URL = {
        test: ctx + "/becTest/test",
        check1:ctx + "/becTest/check1",
        list:ctx + "/becTest/list",
        reset:ctx + "/becTest/reset",
        changeTable:ctx + "/becTest/changeTable",
        importExcel:ctx + "/becTest/importExcel"
    }
    //初始化查询
    this.initQuery = function () {



        //初始化表格
        this.initGrid = function () {
            $('#dataList').datagrid({
                singleSelect: false,
                collapsible: true,
                pagination: true,
                method: 'post',
                remoteSort: false,
                checkOnSelect: true,
                rownumbers: true,
                toolbar: '#tb',
                fit: true,
                pageSize: 25,
                pageList: [25, 50, 100],
                columns: [[
                    {field: 'ck', checkbox: true},
                    {field: 'id',title:'序号'},
                    {field: 'eName', title: '英文', width: 200, align: 'center', sortable: false},
                    {field: 'cName', title: '中文', width: 300, align: 'center', sortable: false},
                    {field: 'state', title: '状态', width: 130, align: 'center',
                        styler: function (value) {
                            if (value == 1) {
                                return 'color:green;';
                            }else {
                                return 'color:red;'
                            }
                        },
                        formatter: function (value) {
                            if (1 == value) return "已掌握";
                            else return "未掌握";
                        }}
                   ]]
            });
        }

    }

    //初始化按钮功能
    this.initFun = function () {

        //查询按钮
        $("#getAllBtn").click(function () {
            $('#dataList').css("display","block");
            $('#test1').css("display","none");
            $('#test2').css("display","none");
            var options = $('#dataList').datagrid('options');
            options.url = contactInfo.URL.list;
            $('#dataList').datagrid("load"); //不要使用$('#dataList').datagrid(options);防止二次渲染
        });

        $("#searchBtn").click(function () {
            $('#dataList').datagrid('loadData', { total: 0, rows: [] });
            totalNum=0;
            correct=0;
            error=0;
            totalNum++;
            $('#dataList').css("display","none");
            $("#testTotal").val(totalNum);
            $("#test1").css("display","block");
            $("#test2").css("display","none");
            $("#tip1").html("");
            $("#answer").css("display","none");
            $.ajax({
                type: "POST",
                url: contactInfo.URL.test,
                dataType: "json",
                success: function (data) {
                    if (data.type=='error' ){
                        $.msgbox({msg: data.text, icon: data.type});
                    } else {
                        var quest=parseInt(Math.random()*3);
                        $("#questId").val(data[quest].id);
                        $("#answer").val(data[quest].cName);
                        $("#answerId").val(quest);
                        $("#quest").html(data[quest].eName);

                        $("#choiceA").val(data[0].cName);
                        $("#choiceB").val(data[1].cName);
                        $("#choiceC").val(data[2].cName);
                        $("#choiceD").val(data[3].cName);
                    }
                }
            });
        });

        $("#spellBtn").click(function () {
            totalNum=0;
            correct=0;
            error=0;
            totalNum++;
            $('#dataList').datagrid('loadData', { total: 0, rows: [] });
            $('#dataList').css("display","none");
            $("#spell").val("");
            $("#testTotal").val(totalNum);
            $("#test2").css("display","block");
            $("#test1").css("display","none");
            $("#tip2").html("");
            $("#answer2").css("display","none");
            $.ajax({
                type: "POST",
                url: contactInfo.URL.test,
                dataType: "json",
                success: function (data) {
                    if (data.type=='error' ){
                        $.msgbox({msg: data.text, icon: data.type});
                    } else {
                        var quest=parseInt(Math.random()*3);
                        if (type=="e"){
                            $("#quest2").html(data[quest].eName);
                            $("#answer2").val(data[quest].cName);
                            $("#questId2").val(data[quest].id);
                        } else {
                            $("#quest2").html(data[quest].cName);
                            $("#answer2").val(data[quest].eName);
                            $("#questId2").val(data[quest].id);
                        }
                    }

                }
            });
        });

        $("#EC").click(function () {
             if (type=='e'){
                 type='c';
             } else {
                 type='e'
             }

            totalNum=0;
            correct=0;
            error=0;
            totalNum++;
            $("#spell").val("");
            $("#testTotal").val(totalNum);
            $("#test2").css("display","block");
            $("#test1").css("display","none");
            $("#tip2").html("");
            $("#answer2").css("display","none");
            $.ajax({
                type: "POST",
                url: contactInfo.URL.test,
                dataType: "json",
                success: function (data) {
                    var quest=parseInt(Math.random()*3);
                    if (type=="e"){
                        $("#quest2").html(data[quest].eName);
                        $("#answer2").val(data[quest].cName);
                    } else {
                        $("#quest2").html(data[quest].cName);
                        $("#answer2").val(data[quest].eName);
                    }
                }
            });
        });


        //重置
        $("#reset").click(function () {
            $.messager.confirm('重置确认', '确认重置所有单词?', function (r) {
                if (r) {
                    $.post(contactInfo.URL.reset, function (data) {
                        $.msgbox({msg: data.text, icon: data.type});
                        if (data.type == 'success') {
                            $('#dataList').datagrid('reload');
                        }
                    });
                }
            });
        });

        //表切换
        $("#changeTable").click(function () {
            $.messager.confirm('表切换确认', '确认切换单词表?', function (r) {
                var table=""
                if (r) {
                    $.post(contactInfo.URL.changeTable, function (data) {
                        $.msgbox({msg: data.text, icon: data.type});
                        if (data.type == 'success') {
                            $('#dataList').datagrid('reload');
                        }
                    });
                }
            });
        });

        //导入数据
        $("#importBtn").click(function () {
            //打开层
            $('#uploadPlanForm').form('clear');
            $(".am-gallery-item").remove();
            $('#uploadPlanDialog').dialog("open");
        });

        //导入按钮
        $("#importOKBtn").click(function () {
            $('#uploadPlanForm').form('submit', {
                url: contactInfo.URL.importExcel,
                onSubmit: function () {
                    if ($(this).form('enableValidation').form('validate')) {
                        $.messager.progress({
                            title: '提示',
                            msg: '正在上传，请稍候...'
                        });
                        return true;
                    }
                    return false;
                },
                success: function (data) {
                    $.messager.progress('close');
                    var data = eval('(' + data + ')'); // change the JSON string to javascript object
                    if (data.type == 'success') {
                        $.msgbox({msg: data.text, icon: data.type});
                        $('#dataList').datagrid("reload");
                        $('#uploadPlanDialog').dialog('close');
                    } else if (data.type == 'warning') {
                        $('#dataList').datagrid("reload");
                        $('#uploadPlanDialog').dialog('close');
                        $('#errormsg').html(data.text);
                        $('#excelDialog').dialog("open");
                    }
                }, onLoadError: function () {
                    $.messager.progress('close');
                    $.msgbox({msg: '上传失败!', icon: 'error'});
                }
            });
        })

    }

    this.init = function () {
        contactInfo.initQuery();
        contactInfo.initGrid()
        contactInfo.initFun();


    }

};

$(function () {
    new contact().init();

});

var totalNum=0;
var correct=0;
var error=0;
var type='e';


function check1(val) {
    var questId=$("#questId").val();
    var answerId=$("#answerId").val();

    if (val==answerId) {
        $.ajax({
            type: "POST",
            url: ctx + '/becTest/check1',
            data:{questId:questId},
            dataType: "json",
            success: function (data) {
                correct++;
                $("#testTrue").val(correct);
                $("#tip1").css("color","green");
                $("#tip1").html("正确");
                $("#answer").css("display","block");
            }
        });


    }else {
        error++;
        $("#testError").val(error);
        $("#tip1").css("color","red");
        $("#tip1").html("错误")
        $("#answer").css("display","block");
    }
}

function check2() {
    var questId=$("#questId2").val();
    var spell=$("#spell").val();
    var answer2=$("#answer2").val();

    if (spell=="" || spell==null) {
        $("#tip2").css("color","red");
        $("#tip2").html("请输入答案！")
        return;
    }

    if(type=='e'){
        if (answer2.search(spell)!=-1) {
            $.ajax({
                type: "POST",
                url: ctx + '/becTest/check1',
                data:{questId:questId},
                dataType: "json",
                success: function (data) {
                    correct++;
                    $("#testTrue").val(correct);
                    $("#tip2").css("color","green");
                    $("#tip2").html("正确");
                    $("#answer2").css("display","block");
                }
            });

        }else {
            error++;
            $("#testError").val(error);
            $("#tip2").css("color","red");
            $("#tip2").html("错误")
            $("#answer2").css("display","block");
        }
    }else {
        if (spell==answer2) {
            $.ajax({
                type: "POST",
                url: ctx + '/becTest/check1',
                data:{questId:questId},
                dataType: "json",
                success: function (data) {
                    correct++;
                    $("#testTrue").val(correct);
                    $("#tip2").css("color","green");
                    $("#tip2").html("正确");
                    $("#answer2").css("display","block");
                }
            });
        }else {
            error++;
            $("#testError").val(error);
            $("#tip2").css("color","red");
            $("#tip2").html("错误")
            $("#answer2").css("display","block");
        }
    }


}


function getOne() {
    totalNum++;
    $("#testTotal").val(totalNum);
    $("#test1").css("display","block");
    $("#tip1").html("");
    $("#answer").css("display","none");
    $.ajax({
        type: "POST",
        url: ctx + "/becTest/test",
        dataType: "json",
        success: function (data) {
            if (data.type=='error' ){
                $.msgbox({msg: data.text, icon: data.type});
            } else {
                var quest=parseInt(Math.random()*3);
                $("#questId").val(data[quest].id);
                $("#answer").val(data[quest].cName);
                $("#answerId").val(quest);
                $("#quest").html(data[quest].eName);

                $("#choiceA").val(data[0].cName);
                $("#choiceB").val(data[1].cName);
                $("#choiceC").val(data[2].cName);
                $("#choiceD").val(data[3].cName);
            }
        }
    });
}


function getOne2() {
    totalNum++;
    $("#spell").val("");
    $("#testTotal").val(totalNum);
    $("#test2").css("display","block");
    $("#tip2").html("");
    $("#answer2").css("display","none");
    $.ajax({
        type: "POST",
        url: ctx + '/becTest/test',
        dataType: "json",
        success: function (data) {
            if (data.type=='error' ){
                $.msgbox({msg: data.text, icon: data.type});
            } else {
                var quest=parseInt(Math.random()*3);
                if (type=='e'){
                    $("#quest2").html(data[quest].eName);
                    $("#answer2").val(data[quest].cName);
                    $("#questId2").val(data[quest].id);
                } else {
                    $("#quest2").html(data[quest].cName);
                    $("#answer2").val(data[quest].eName);
                    $("#questId2").val(data[quest].id);
                }
            }


        }
    });
}


function getkeys() {
    if (event.keyCode!==13){
        return;
    }

    var questId=$("#questId2").val();
    var spell = $("#spell").val();
    var answer2 = $("#answer2").val();
    if (spell == "" || spell == null) {
        $("#tip2").css("color", "red");
        $("#tip2").html("请输入答案！")
        return;
    }
    if(type=='e'){
        if (answer2.search(spell)!=-1) {
            $.ajax({
                type: "POST",
                url: ctx + '/becTest/check1',
                data:{questId:questId},
                dataType: "json",
                success: function (data) {
                    correct++;
                    $("#testTrue").val(correct);
                    $("#tip2").css("color","green");
                    $("#tip2").html("正确");
                    $("#answer2").css("display","block");
                }
            });

        }else {
            error++;
            $("#testError").val(error);
            $("#tip2").css("color","red");
            $("#tip2").html("错误")
            $("#answer2").css("display","block");
        }
    }else {
        if (spell==answer2) {
            $.ajax({
                type: "POST",
                url:ctx + '/becTest/check1',
                data:{questId:questId},
                dataType: "json",
                success: function (data) {
                    correct++;
                    $("#testTrue").val(correct);
                    $("#tip2").css("color","green");
                    $("#tip2").html("正确");
                    $("#answer2").css("display","block");
                }
            });
        }else {
            error++;
            $("#testError").val(error);
            $("#tip2").css("color","red");
            $("#tip2").html("错误")
            $("#answer2").css("display","block");
        }
    }
}


function search1(val) {
    debugger
    var word=val.innerHTML
    var http="https://fanyi.baidu.com/?aldtype=16047#auto/zh/"+word;
    window.open(http);
}